package com.stir.cgt.ajm.client;

import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.RootLayoutPanel;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CGT_GWT implements EntryPoint, ValueChangeHandler<String> {

	private HorizontalPanel buttons = new HorizontalPanel();;
	private Hyperlink continueLink,backLink;
	private ArrayList<MyLayoutPanel> panels = new ArrayList<MyLayoutPanel>();
	private DockLayoutPanel mainPanel = new DockLayoutPanel(Unit.EM);;
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Clear any previous history tokens
		History.newItem("0");		

		setPanels();		

		// Add history listener
		History.addValueChangeHandler(this);
		// Fire the initial history state.
		History.fireCurrentHistoryState();
	}

	/**
	 * Initiate the panels which the user will navigate
	 */
	private void setPanels() {
		// Set the navigation links
		continueLink=new Hyperlink("<h2 style=\"color:green; text-align:right\">Continue</h2>",true,"0");
		backLink=new Hyperlink("<h2 style=\"color:green\">Back<h2>",true,"0");
		mainPanel.addSouth(buttons, 3);
		
		Test buckets = new Test();
		buckets.setData(new TreeData(), false);
		panels.add(new Intro("/files/help/cgtIntro.html"));
		panels.add(new Intro("/files/help/watIntro.html"));
		panels.add(new Intro("/files/help/utIntro.html"));
		panels.add(buckets);
		panels.add(new Intro("/files/help/scaleIntro.html"));
		panels.add(new Intro("/files/help/satIntro.html"));
		
		// Add the main panel to the root panel
		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(mainPanel);
	}

	/**
	 * Called when the history changes, either by the user progressing or using
	 * the browsers forward and back buttons
	 */
	public void onValueChange(ValueChangeEvent<String> event) {
		try {
			// Get the current state and calculate next and previous states
			String current = event.getValue();
			String next = String.valueOf(Integer.parseInt(current)+1);
			String previous = String.valueOf(Integer.parseInt(current)-1);
			
			//Update the hyperlinks
			continueLink.setTargetHistoryToken(next);
			backLink.setTargetHistoryToken(previous);

			// Remove the back or continue buttons if necessary
			buttons.clear();
			if(current.equals("0"))
				buttons.add(continueLink);
			else if (next.equals(String.valueOf(panels.size())))
				buttons.add(backLink);
			else {
				buttons.add(backLink);
				buttons.add(continueLink);
			}

			// Update to display the new state
			mainPanel.clear();
			mainPanel.addSouth(buttons,3);
			panels.get(Integer.parseInt(current)).refresh();
			mainPanel.add(panels.get(Integer.parseInt(current)));
		}
		catch (Exception e){
			//Exceptions can be caused by bookmarks or history manipulation
			History.newItem("0");
			History.fireCurrentHistoryState();
		}
	}
}
